//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Mindscape.BackgroundMotion.Model;
using Mindscape.BackgroundMotion.Website.Controllers;
using Mindscape.BackgroundMotion.Website.Controls;
using Mindscape.BackgroundMotion.Website.Infrastructure;
using Mindscape.BackgroundMotion.Website.Views;

namespace Mindscape.BackgroundMotion.Website
{
  [CodeViewer("We used a provider model to allow any number of external resources to be plugged in to the system. Lets explore how the Flickr provider has been added to the page using this approach.")]
  [CodeViewerReference(1, @"\Clients\Website\Add.aspx", 16, 26, "Each of the providers is specifically added on to the Add view as a user control, this keeps a clean seperation for the provider UI")]
  [CodeViewerReference(2, @"\Clients\Website\Controls\FlickrSearch.ascx", 4, 10, "On the provider's control UI we use an ASP.NET AJAX modal popup control to load each of the provider user interfaces into a modal dialog. This is really simple using the AJAX control!")]
  [CodeViewerReference(3, @"\Clients\Website\Controls\FlickrSearch.ascx.cs", 119, 161, "When you click Search on the Flickr control, it calls to the Flickr API and then raises an event which has been subscribed to by the View")]
  [CodeViewerReference(4, @"\Clients\Website\Add.aspx.cs", 115, 118, "The event is wired up to call a Presenter method which will try to find the requested item through the provider model given the name of the provider and a piece of information the provider will be able to use")]
  [CodeViewerReference(5, @"\Clients\Website\Controllers\ContributionController.cs", 55, 61, "The call ultimately gets handled by the Controller which will find the associated provider and ask it to return a description of the resource details matching the identifier")]  
  [CodeViewerReference(6, @"\Clients\Website\web.config", 39, 44, "The providers are declared in the web.config file")]
  [CodeViewerReference(7, @"\Clients\Website\Resources\Flickr.cs", 19, 60, "The Flickr provider uses the Flickr API to find the item and then build up a description of the resource")]
  [CodeViewerReference(8, @"\Clients\Website\Presenters\AddPresenter.cs", 52, 56, "The Contribution entity which is returned from the Controller call is then rebound to the View which causes the page to update the data displayed in the form. We use an ASP.NET AJAX UpdatePanel control to remove the need for a visible post back")]
  public partial class Add : PageBase<AddPresenter, IAddView, IContributionController>, IAddView
  {
    private Contribution _contribution;

    /// <summary>
    /// The contribution currently being entered in to the system
    /// </summary>
    public Contribution Contribution
    {
      get { return _contribution; }
      set { _contribution = value; }
    }

    /// <summary>
    /// A list of valid content types which can be associated with the new contribution
    /// </summary>
    public IList<ContentType> ContentTypes
    {
      set
      {
        if (!IsPostBack)
        {
          BindDropDownList(ContributionContentType, "Id", "Description", value);
        }
      }
    }

    /// <summary>
    /// A list of validation results to be shown in error if there are data entry errors on the contribution
    /// </summary>
    public ValidationResults ValidationResults
    {
      set
      {
        ErrorsRepeater.DataSource = value;
        ErrorsRepeater.DataBind();
      }
    }

    /// <summary>
    /// Conditionally shows the data entry part of the form
    /// </summary>
    public void ShowForm()
    {
      //SelectionSection.Visible = false;
      DataBinder.DataBind();
      ContentPanel.Update();
    }

    /// <summary>
    /// Raises the <see cref="E:Load"/> event.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      // Load required virtual earth integration
      ((MasterPages.Default)Master).Body.Attributes.Add("onload", "GetMap();");
      ((MasterPages.Default)Master).EnableVirtualEarthIntegration = true;

      // Wire up 
      FlickrSearch.PhotoSelected += new FlickrSearch.PhotoSelectedDelegate(FlickrSearch_PhotoSelected);
      SilverStream.AppSelected += new SilverlightStreaming.SilverlightAppSelectedDelegate(SilverStream_AppSelected);
      LivePhotos.PhotoSelected += new LiveSpacesPhotos.LiveSpacesPhotoSelectedDelegate(LivePhotos_PhotoSelected);
    }

    void LivePhotos_PhotoSelected(object sender, EventArgs e)
    {
      DataBinder.Unbind();
      Presenter.LoadContributionFromProvider("LivePhotos", LivePhotos.PhotoUrl);
    }

    void SilverStream_AppSelected(object sender, EventArgs e)
    {
      DataBinder.Unbind();
      Presenter.LoadContributionFromProvider("Silverlight", String.Concat(((SilverlightStreaming)sender).SelectedId, "/", ((SilverlightStreaming)sender).SelectedAppName));
    }

    /// <summary>
    /// Handles the PhotoSelected event of the FlickrSearch control which causes the form to be populated by the Flickr provider
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void FlickrSearch_PhotoSelected(object sender, EventArgs e)
    {
      DataBinder.Unbind();
      Presenter.LoadContributionFromProvider("Flickr", ((FlickrSearch)sender).SelectedId);
    }

    /// <summary>
    /// Handles the Click event of the SelfHosted control which allows the data entry form to be shown
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelfHosted_Click(object sender, EventArgs e)
    {
      ShowForm();
    }

    /// <summary>
    /// Handles the Click event of the Create control which causes the new contribution to be saved
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Create_Click(object sender, EventArgs e)
    {
      DataBinder.Unbind();
      Presenter.Save();
    }
  }
}